/*
Copyright (C) 2007 Kamal Singh

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

     http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

// http://www.codeproject.com/jscript/wmiadmin.asp

var ip_address  = "127.0.0.1";
var wmi_service = null;

utils.setInterval(view_onOpen, 1000);
utils.setInterval(changeMessage, 1000 * 60);

function view_onOpen()
{
	// WMI setup
	wmi_service = GetObject("winmgmts:{impersonationLevel=impersonate}!//" + ip_address + "/root/cimv2");
	gatherOsInfo();
}

var _minimized = false;

function view_onminimize()
{
  _minimized = true;
  gatherOsInfo();
}

function view_onrestore()
{
  _minimized = false;
  view.caption = GADGET_NAME;
}

function changeMessage()
{
  var ran_unrounded = Math.random() * 3;
  var ran_number = Math.floor(ran_unrounded);
  
  if(ran_number == 0)
    message.innerText = "System running for";
  if(ran_number == 1)
    message.innerText = "Haven't slept for";
  if(ran_number == 2)
    message.innerText = "WoW Windows up for";
}

function gatherOsInfo()
{
	var fc = new Enumerator(runQuery("Win32_OperatingSystem"));

	var numItems = 0;
	for (; !fc.atEnd(); fc.moveNext())
	{
		numItems++;
	}

	if(numItems > 0)
	{
		for(fc.moveFirst(); !fc.atEnd(); fc.moveNext())
		{
			var Obj = fc.item();
			var date_str = String(Obj.LastBootUpTime);

			var boot_time = new Date();
			boot_time.setMonth(date_str.substring(4, 6) - 1);
			boot_time.setDate(date_str.substring(6, 8));
			boot_time.setFullYear(date_str.substring(0, 4));
			boot_time.setHours(date_str.substring(8, 10)); 
			boot_time.setMinutes(date_str.substring(10, 12));
			boot_time.setSeconds(date_str.substring(12, 14));

			var current_time = new Date();
			var difference = (current_time - boot_time)/1000;

			intSecsTotal     = difference;
			intDays          = Math.floor(intSecsTotal / 86400);
			intSecsRemaining = (intSecsTotal - (intDays * 86400));
			intSecsTotal     = intSecsRemaining;
			intHours         = Math.floor(intSecsTotal / 3600);
			intSecsRemaining = (intSecsTotal - (intHours * 3600));
			intSecsTotal     = intSecsRemaining;
			intMinutes       = Math.floor(intSecsTotal / 60);
			intSeconds       = Math.floor(intSecsTotal - (intMinutes * 60));
		 
			daysLabel.innerText = intDays;
			hoursLabel.innerText = intHours;
			minutesLabel.innerText = intMinutes;
			seondsLabel.innerText = intSeconds;
      if ( _minimized )
      {
        view.caption = intDays + " d " +intHours + " h " + intMinutes + " m "+ intSeconds + " s";
      }

		}
	}
}

// query routine
function runQuery(className)
{
	return wmi_service.ExecQuery("Select * from " + className);
}